<?php

declare(strict_types=1);

namespace App\AdminApi\Modular\Administer\Validate;

use App\Common\Modular\Administer\Enums\UserEnums;
use Zms\Validate\ValidateAbstract;
use Zms\Unit\Lists;

/**
 * 后台管理员验证器
 */
class UserValidate extends ValidateAbstract
{
    public function rules(): array
    {
        return [
            ['id', 'required', 'error' => '数据异常，请刷新重试', 'on' => ['updated']],
            ['id', 'number', 'error' => '数据异常，请刷新重试', 'on' => ['updated']],

            ['nickname', 'required', 'on' => ['created', 'updated']],
            ['nickname', 'string', 'max' => 20, 'on' => ['created', 'updated']],

            ['account', 'required', 'error' => '{label}不能为空', 'on' => ['created']],
            ['account', 'string', 'min' => 5, 'max' => 30, 'on' => ['created', 'updated']],

            ['password', 'required', 'on' => ['created']],
            ['password','string','on'=>['updated']],

            ['avatar', 'required', 'on' => ['created', 'updated']],
            ['avatar', 'string', 'on' => ['created', 'updated']],

            ['job_id', 'required', 'on' => ['created', 'updated']],

            ['department_id', 'list', 'on' => ['created', 'updated']],

            ['is_super', 'in', 'enums' => UserEnums::Values(UserEnums::TAG['SUPER']), 'on' => ['created', 'updated']],

            ['role_id', 'required', 'error' => '请为此账号分配{label}', 'on' => ['created', 'updated']],
            ['role_id', 'list', 'max' => 3, 'on' => ['created', 'updated']],

            ['data_auth_type', 'required', 'error' => '请选择{label}', 'on' => ['created', 'updated']],
            ['data_auth_type', 'in', 'enums' => UserEnums::Values(UserEnums::TAG['DATA_AUTH_TYPE']), 'on' => ['created', 'updated']],

            ['auth_dep', 'list', 'on' => ['created', 'updated']]
        ];
    }


    public function labels(): array
    {
        return [
            'nickname' => '管理员昵称',
            'account' => '登录账号',
            'password' => '登录密码',
            'avatar' => '账号头像',
            'department_id' => '所属部门',
            'job_id' => '所在岗位',
            'is_super' => '超级权限',
            'role_id' => '角色',
            'data_auth_type' => '数据权限类型',
            'auth_dep' => '授权部门',
        ];
    }

    /**
     * 数据验证权限
     * @param string $find
     * @param array $data
     * @return bool
     */
    public function where(string $find, array $data): bool
    {
        if ($this->getScene() === 'created') {
            return true;
        }
        if (in_array($find, ['data_auth_type', 'auth_dep'])) {
            return false;
        }
        return parent::where($find, $data);
    }


    public function afterValidate(array $data, array $safe): array
    {
        if (is_array($safe['department_id']) && count($safe['department_id']) > 0) {
            $safe['department_id'] = Lists::last($safe['department_id']);
        } else {
            $safe['department_id'] = '';
        }
        return parent::afterValidate($data, $safe); // TODO: Change the autogenerated stub
    }
}